package cn.frt.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.frt.pojo.Coupons;
import cn.frt.pojo.Shops;
import cn.frt.service.CouponsService;

@Controller
@RequestMapping(value="/coupons")
public class CouponsController {
	@Resource
	private CouponsService couponsService;
	
	@RequestMapping(value="/entranceaddcoupons")
	public String entranceAddCoupons(){
		return "/coupons/coupons_add";
	}
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(true);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	@RequestMapping(value="/addcoupons")
	public String addCoupons(Coupons coupons){
		int cc=couponsService.insertCoupons(coupons);
		if(cc>0){
			return "/coupons/coupons_add";
		}
		return "/coupons/coupons_add";
	}
	@RequestMapping(value="/entrancecouponsselect",method=RequestMethod.GET)
	public String entranceCouponsSelect(){
		return "/coupons/coupons_surface";
	}
	@RequestMapping(value="/couponsselect",method=RequestMethod.POST)
	@ResponseBody
	public Object couponsSelect(){
	List<Coupons> list=couponsService.selectCoupons();
		return list;
	}
	@RequestMapping(value="/entranceupdate")
	public String selectUpdate(Integer couponsId,Model model){
		List<Coupons> list=	couponsService.selectCouponsById(couponsId);
			model.addAttribute("coupons", list);
			return "/coupons/coupons_update";
		}
	//修改
	@RequestMapping(value="/updatacoupons",method=RequestMethod.POST)
	public String updateCoupons(Coupons coupons){
		if(coupons==null){
			return "error";
		}else{
			couponsService.updateCouponsById(coupons);
			return "/coupons/coupons_surface";
		}
	}
	//删除运营
		@RequestMapping(value="/deletecoupons")
		public String deleteCoupons(Integer couponsId){
			if(couponsId==null){
				return "error";
			}else{
				couponsService.deleteCouponsById(couponsId);
				return "/coupons/coupons_surface";
			}
		}
	
}
